class Music extends egret.DisplayObjectContainer {
    private bgm
    private soundChannel = null
    private btn_sound = new egret.Shape()
    public constructor() {
        super()
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this)
    }
    private init() {
        this.addChild(this.btn_sound)
        this.loadBgm()
        // this.createBtnPlay()
        this.createMask()
    }
    // 加载背景音乐资源
    private loadBgm() {
        this.bgm = RES.getRes('bgm_mp3')
    }
    // 创建播放遮罩
    private createMask() {
        $('#miva_input').hide()
        $('#miva_input_div').hide()
        const _mask = new egret.Sprite()
        this.addChild(_mask)
        _mask.graphics.beginFill(0, .3)
        _mask.graphics.drawRect(0, 0, this.stage.stageWidth, this.stage.stageHeight)
        _mask.graphics.endFill()
        const text = Utils.createText('点击开启背景音乐', { x: 100, y: 590 }, _mask, false)
        _mask.addChild(text)
        _mask.touchEnabled = true
        _mask.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            console.log('播放bgm')
            // this.createBtnPause()
            this.soundChannel = this.bgm.play()
            this.removeChild(_mask)
            // $('#miva_input').show()
            $('#miva_input_div').show()
        }, this)
    }
    // 创建播放按钮
    private createBtnPlay() {
        const btn_sound = new egret.Shape()
        this.removeChild(this.btn_sound)
        this.addChild(btn_sound)
        this.btn_sound = btn_sound

        this.btn_sound.graphics.lineStyle(5, 0xffffff)
        this.btn_sound.graphics.beginFill(0, .3)
        this.btn_sound.graphics.drawArc(50, 50, 30, 0, Math.PI / 180 * 360, false)
        this.btn_sound.graphics.endFill()
        this.btn_sound.graphics.moveTo(45, 35)
        this.btn_sound.graphics.lineTo(45, this.btn_sound.height)
        this.btn_sound.graphics.endFill()
        this.btn_sound.graphics.moveTo(55, 35)
        this.btn_sound.graphics.lineTo(55, this.btn_sound.height)
        this.btn_sound.graphics.endFill()

        this.btn_sound.touchEnabled = true
        this.btn_sound.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            console.log('播放bgm')
            this.createBtnPause()
            this.soundChannel = this.bgm.play()
        }, this)

        this.settingBenPath()
    }
    // 创建暂停按钮
    private createBtnPause() {
        const btn_sound = new egret.Shape()
        this.removeChild(this.btn_sound)
        this.addChild(btn_sound)
        this.btn_sound = btn_sound

        this.btn_sound.graphics.lineStyle(5, 0xffffff)
        this.btn_sound.graphics.beginFill(0, .3)
        this.btn_sound.graphics.drawArc(50, 50, 30, 0, Math.PI / 180 * 360, false)
        this.btn_sound.graphics.endFill()
        this.btn_sound.graphics.lineStyle(5, 0xffffff)
        this.btn_sound.graphics.drawArc(40, 50, 0, -45, Math.PI / 180 * 60, false)
        this.btn_sound.graphics.endFill()
        this.btn_sound.graphics.lineStyle(5, 0xffffff)
        this.btn_sound.graphics.drawArc(40, 50, 10, -45, Math.PI / 180 * 60, false)
        this.btn_sound.graphics.endFill()
        this.btn_sound.graphics.lineStyle(5, 0xffffff)
        this.btn_sound.graphics.drawArc(40, 50, 20, -45, Math.PI / 180 * 60, false)
        this.btn_sound.graphics.endFill()

        this.btn_sound.touchEnabled = true
        this.btn_sound.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            console.log('暂停bgm')
            this.createBtnPlay()
            this.soundChannel.stop()
            this.soundChannel = null
        }, this)

        this.settingBenPath()
    }
    // 设置按钮位置
    private settingBenPath() {
        Utils.anchorCenter(this.btn_sound)
        this.btn_sound.x = this.stage.stageWidth - this.btn_sound.width - 50
        this.btn_sound.y = 50
    }
}